﻿using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AppLab.Web;
using AppLab.Data.Models;

namespace AppLab.Controls
{
    public class UploadTextBox : TextBox
    {
        protected SubmitData submitData;

        public FileInfo selectedFile;

        public UploadTextBox(SubmitData e)
        {
            submitData = e;

            base.AllowDrop = true;
            base.DragEnter += TextBox_DragEnter;
            base.DragOver += TextBox_DragOver;
            base.DragLeave += TextBox_DragLeave;
            base.Drop += TextBox_Drop;
            //base.TextChanged += TextBox_TextChanged;
        }

        //void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        //{
        //    if (selectedFile != null)
        //    {
        //        if (string.IsNullOrEmpty(base.Text.Replace(" ","")))
        //        {
        //            submitData.PackageContent = null;
        //        }
        //    }
        //    else
        //    {  
        //        base.Text = string.Empty;
        //    }
        //}

        private void TextBox_DragEnter(object sender, DragEventArgs e)
        {

        }

        private void TextBox_DragOver(object sender, DragEventArgs e)
        {
            base.Background.Opacity = 0.5;
        }

        private void TextBox_DragLeave(object sender, DragEventArgs e)
        {
            base.Background.Opacity = 1;
        }

        private void TextBox_Drop(object sender, DragEventArgs e)
        {
            FileInfo[] sources = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];
            if (sources != null && sources.Count() > 0)
            {
                if (sources[0].Exists)
                {
                    selectedFile = sources.First();
                    //base.Text = String.Format("{0} ({1}字节)", fi.Name, fi.Length);
                    base.Text = selectedFile.Name;
                    UpdateFileContent(selectedFile);
                }
            }
        }

        public virtual void UpdateFileContent(FileInfo fi)
        {
        }
    }
}
